package com.bbx.taxi.client.Activity.Address;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import butterknife.InjectView;

import com.bbx.api.gclient.GMsg;
import com.bbx.taxi.official.R;
import com.bbx.taxi.client.Activity.Base.BaseBBXActivity;

public class AddAddressActivity extends BaseBBXActivity {
	//
	@InjectView(R.id.addressbook)
	Button addressbook;
	@InjectView(R.id.telephone)
	EditText telephone;
	String username, usernumber;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.addaddress);
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.addressbook:
			startActivityForResult(new Intent(Intent.ACTION_PICK,
					ContactsContract.Contacts.CONTENT_URI), 0);
			break;
		default:
			super.onClick(v);
		}
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		addressbook.setOnClickListener(this);
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub

	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) {
			ContentResolver reContentResolverol = getContentResolver();
			Uri contactData = data.getData();
			@SuppressWarnings("deprecation")
			Cursor cursor = managedQuery(contactData, null, null, null, null);
			cursor.moveToFirst();
			username = cursor.getString(cursor
					.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
			String contactId = cursor.getString(cursor
					.getColumnIndex(ContactsContract.Contacts._ID));
			Cursor phone = reContentResolverol.query(
					ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
					ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
							+ contactId, null, null);
			while (phone.moveToNext()) {
				usernumber = phone
						.getString(phone
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
				telephone.setText(usernumber + " (" + username + ")");
			}
			cursor.close();
			phone.close();
		}
	}
	@Override
	protected void desotryItems() {
		// TODO Auto-generated method stub

	}
}
